package com.baidu.automata.core.flow;

import java.util.HashMap;
import java.util.Map;

import com.baidu.automata.core.context.Context;
import com.baidu.automata.core.type.BasicDataField;
import com.baidu.automata.core.type.DataField;

@SuppressWarnings("rawtypes")
public abstract class BaseOpStep {
	protected String id = null;

	protected Context ctx = null;

	protected Map<String, String> forwards = new HashMap<String, String>();

	public BaseOpStep() {
		super();
	}

	public BaseOpStep(String id) {
		super();
		this.id = id;
	}

	public Context getCtx() {
		return ctx;
	}

	public void setCtx(Context ctx) {
		this.ctx = ctx;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void addDataField(BasicDataField f) {
		this.ctx.addDataField(f);
	}

	public DataField getDataFieldById(String id) {
		return this.ctx.getDataFieldById(id);
	}

	public DataField removeDataFieldById(String id) {
		return this.ctx.removeDataFieldById(id);
	}

	public void addForward(String exitCode, String stepId) {
		this.forwards.put(exitCode, stepId);
	}

	public void removeForward(int exitCode) {
		this.forwards.remove(String.valueOf(exitCode));
	}

	public String getForwardOpStepId(int exitCode) {
		String key = String.valueOf(exitCode);
		if (this.forwards.containsKey(key)) {
			return this.forwards.get(key);
		} else if (this.forwards.containsKey("other")) {
			return this.forwards.get("other");
		} else {
			return "end";
		}
	}

	protected abstract int execute();
}
